package com.example.opengldemo.vertex_magical.shader;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.opengl.GLES30;
import android.opengl.GLUtils;

import com.example.opengldemo.R;
import com.example.opengldemo.util.MatrixState;
import com.example.opengldemo.util.TextureUtil;

public class TwistRenderer extends BaseRenderer {
    protected TwistRenderer(Resources resource, String vertex, String fragment) {
        super(resource, vertex, fragment);
    }
    final float Y_MAX=1.5f;
    final float Y_MIN=-1.5f;
    final float hw=0.575f;

    float angleSpan= 0f;
    float ySpan= 2f;


    @Override
    protected void onCreate() {
        super.onCreate();
        getVertex();
        shaderHandle.UHandleArray[0] = shaderHandle.getUHandle("angleSpan");
        shaderHandle.UHandleArray[1] = shaderHandle.getUHandle("yStart");
        shaderHandle.UHandleArray[2] = shaderHandle.getUHandle("ySpan");
        Bitmap bmp = BitmapFactory.decodeResource(mRes, R.drawable.guoqi);
        if (bmp != null) {
            if (markTextureId == -1) {
                markTextureId = TextureUtil.createTextureID(false);
            } else {
                GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, markTextureId);
            }
            GLUtils.texImage2D(GLES30.GL_TEXTURE_2D, 0, bmp, 0);
            bmp.recycle();
        } else {
            if (markTextureId != -1) {
                GLES30.glDeleteTextures(1, new int[]{markTextureId}, 0);
            }
        }
        new Thread()
        {
            public void run()
            {
                while(true)
                {
                    angleSpan+=(float) (Math.PI/16);
                    try
                    {
                        Thread.sleep(50);
                    } catch (InterruptedException e)
                    {
                        e.printStackTrace();
                    }
                }
            }
        }.start();


    }

    @Override
    protected void onClear() {
    }
    float ratio  ;
    @Override
    protected void onSizeChanged(int width, int height) {
        super.onSizeChanged(width, height);
        ratio  = (float)width / height ;
    }

    @Override
    protected void onSetExpandData() {
        super.onSetExpandData();

        GLES30.glUniform1f(shaderHandle.UHandleArray[0] , angleSpan);
        GLES30.glUniform1f(shaderHandle.UHandleArray[1] , Y_MIN);
        GLES30.glUniform1f(shaderHandle.UHandleArray[2] , ySpan);

        MatrixState.setProjectFrustum(-ratio, ratio, -1, 1, 2, 8);
        MatrixState.setCamera(0,0, 8, 0f, 0f, 0f, 0f, 1.0f, 0.0f);

        MatrixState.pushMatrix() ;
        MatrixState.translate(0 , 0 , MatrixState.translate[2] );
        MatrixState.rotate(MatrixState.rotate[0] , 1 , 0 , 0 );
        MatrixState.rotate(MatrixState.rotate[1] , 0 , 1 , 0 );
        shaderHandle.bindMVPMaxtrixHandle() ;
        MatrixState.popMatrix() ;

    }

    @Override
    protected void onBindTexture(int textureId) {
        shaderHandle.bindDefaultTextureHandle(markTextureId) ;
    }

    private int markTextureId = -1;
    public void getVertex() {
        int FD = 6;
        float vertices[] = new float[FD * 4 * 6 * 3];
        float texCoor[] = new float[FD * 4 * 6 * 2];
        float yStart = Y_MIN;
        float ySpan = (Y_MAX - Y_MIN) / FD;
        int count = 0;
        int tCount = 0;
        for (int i = 0; i < FD; i++) {
            float x1 = -hw;
            float y1 = yStart;
            float z1 = hw;

            float x2 = hw;
            float y2 = yStart;
            float z2 = hw;

            float x3 = hw;
            float y3 = yStart;
            float z3 = -hw;

            float x4 = -hw;
            float y4 = yStart;
            float z4 = -hw;

            float x5 = -hw;
            float y5 = yStart + ySpan;
            float z5 = hw;

            float x6 = hw;
            float y6 = yStart + ySpan;
            float z6 = hw;

            float x7 = hw;
            float y7 = yStart + ySpan;
            float z7 = -hw;

            float x8 = -hw;
            float y8 = yStart + ySpan;
            float z8 = -hw;
            //512
            vertices[count++] = x5;
            vertices[count++] = y5;
            vertices[count++] = z5;

            vertices[count++] = x1;
            vertices[count++] = y1;
            vertices[count++] = z1;

            vertices[count++] = x2;
            vertices[count++] = y2;
            vertices[count++] = z2;
            //526
            vertices[count++] = x5;
            vertices[count++] = y5;
            vertices[count++] = z5;
            vertices[count++] = x2;
            vertices[count++] = y2;
            vertices[count++] = z2;
            vertices[count++] = x6;
            vertices[count++] = y6;
            vertices[count++] = z6;
            //623
            vertices[count++] = x6;
            vertices[count++] = y6;
            vertices[count++] = z6;
            vertices[count++] = x2;
            vertices[count++] = y2;
            vertices[count++] = z2;
            vertices[count++] = x3;
            vertices[count++] = y3;
            vertices[count++] = z3;
            //637
            vertices[count++] = x6;
            vertices[count++] = y6;
            vertices[count++] = z6;
            vertices[count++] = x3;
            vertices[count++] = y3;
            vertices[count++] = z3;
            vertices[count++] = x7;
            vertices[count++] = y7;
            vertices[count++] = z7;
            //734
            vertices[count++] = x7;
            vertices[count++] = y7;
            vertices[count++] = z7;
            vertices[count++] = x3;
            vertices[count++] = y3;
            vertices[count++] = z3;
            vertices[count++] = x4;
            vertices[count++] = y4;
            vertices[count++] = z4;
            //748
            vertices[count++] = x7;
            vertices[count++] = y7;
            vertices[count++] = z7;
            vertices[count++] = x4;
            vertices[count++] = y4;
            vertices[count++] = z4;
            vertices[count++] = x8;
            vertices[count++] = y8;
            vertices[count++] = z8;
            //841
            vertices[count++] = x8;
            vertices[count++] = y8;
            vertices[count++] = z8;
            vertices[count++] = x4;
            vertices[count++] = y4;
            vertices[count++] = z4;
            vertices[count++] = x1;
            vertices[count++] = y1;
            vertices[count++] = z1;
            //815
            vertices[count++] = x8;
            vertices[count++] = y8;
            vertices[count++] = z8;
            vertices[count++] = x1;
            vertices[count++] = y1;
            vertices[count++] = z1;
            vertices[count++] = x5;
            vertices[count++] = y5;
            vertices[count++] = z5;

            yStart = yStart + ySpan;

            texCoor[tCount++] = 0;
            texCoor[tCount++] = 0;
            texCoor[tCount++] = 0;
            texCoor[tCount++] = 1;
            texCoor[tCount++] = 1;
            texCoor[tCount++] = 1;

            texCoor[tCount++] = 0;
            texCoor[tCount++] = 0;
            texCoor[tCount++] = 1;
            texCoor[tCount++] = 1;
            texCoor[tCount++] = 1;
            texCoor[tCount++] = 0;

            texCoor[tCount++] = 0;
            texCoor[tCount++] = 0;
            texCoor[tCount++] = 0;
            texCoor[tCount++] = 1;
            texCoor[tCount++] = 1;
            texCoor[tCount++] = 1;

            texCoor[tCount++] = 0;
            texCoor[tCount++] = 0;
            texCoor[tCount++] = 1;
            texCoor[tCount++] = 1;
            texCoor[tCount++] = 1;
            texCoor[tCount++] = 0;

            texCoor[tCount++] = 0;
            texCoor[tCount++] = 0;
            texCoor[tCount++] = 0;
            texCoor[tCount++] = 1;
            texCoor[tCount++] = 1;
            texCoor[tCount++] = 1;

            texCoor[tCount++] = 0;
            texCoor[tCount++] = 0;
            texCoor[tCount++] = 1;
            texCoor[tCount++] = 1;
            texCoor[tCount++] = 1;
            texCoor[tCount++] = 0;

            texCoor[tCount++] = 0;
            texCoor[tCount++] = 0;
            texCoor[tCount++] = 0;
            texCoor[tCount++] = 1;
            texCoor[tCount++] = 1;
            texCoor[tCount++] = 1;

            texCoor[tCount++] = 0;
            texCoor[tCount++] = 0;
            texCoor[tCount++] = 1;
            texCoor[tCount++] = 1;
            texCoor[tCount++] = 1;
            texCoor[tCount++] = 0;
        }

        setVertexCo(vertices);
        setTexCoors(texCoor);
    }
}
